#pragma once
#include<iostream>
#include<string>
using namespace std;

void StringConstruct() {
	string s1();   //s1为空
	string s2("hello");
	string s3(4, 'w');    //s3="wwww";
	string s4("12345", 1, 3);   //s4="234";

	//'' 为字符, ""才是字符串
	//不能直接string s('w');
}

void StringInit() {

	//拷贝赋值
	string s1, s2;
	s1 = "Hello";  // s1 = "Hello"
	s2 = 'K';  // s2 = "K”


	//利用assign函数
	string s3("12345"), s4, s5;
	s4.assign(s3);  // s3 = s1
	s5.assign(s3, 1, 2);  // s5 = "23"，即 s1 的子串(1, 2)
	s5.assign(4, 'K');  // s5 = "KKKK"
	s5.assign("abcde", 2, 3);  // s5 = "cde"，即 "abcde" 的子串(2, 3)

	//利用+
	string s;
	char a[5] = { 'h', 'e', 'l', 'l', 'o' };
	s += a[0];

	//利用substr函数
	string s6, s7;
	s6 = "hello";
	s7 = s6.substr(0, 3);   //s2="hel";
}

void StringOperation() {
	{
		//append
		string s1("123"), s2("abc");
		s1.append(s2);  // s1 = "123abc"
		s1.append(s2, 1, 2);  // s1 = "123bc"	(pos, count) 初始位置, 字符数量
		s1.append(3, 'K');  // s1 = "123KKK"
		s1.append("ABCDE", 2, 3);  // s1 = "123CDE"，添加 "ABCDE" 的子串(2, 3)
	}

	{
		//compare	字典排序
		string s1("abcdefg"), s2("about"), s3("abcdefg");
		int a = s1.compare(s2);		//a = -1
		int b = s2.compare(s1);		//a = 1
		int c = s1.compare(s3);		//c = 0

		int d = s1.compare(2, 4, s2);//s1下标为2的字符a开始的4个字符cdef和s2进行比较。cdef大于s2故返回1
		cout << c << endl;

		int e = s1.compare(2, 4, s2, 1, 3);//s1下标为2的字符a开始的4个字符cdef和s2下标为1的字符b开始的3个字符bou比较。前者小，故返回-1。

		string s4 = "abc";
		string s5 = "abc";
		int f = s4.compare(s5);//相等返回0
	}

	{
		//swap
		string s1("abc"), s2("1234");
		s1.swap(s2);	//s1 = "1234"	s2 = "abc"
	}

	{
		//find
		string s = "hello world";
		int a = s.find("e");	//a = 1, 返回第一个找到的下标
		int b = s.find("a");	//b = s.npos	找不到结果
		int c = s.find("ll", 0, 1);	//c = 2		(sub, pos, count)	子串, 被查找字符串起始位置, 子串数量

		int d = s.find_first_of("l");
		int f = s.find_last_of("l");

		int g = s.rfind("o");	//g = 7 reverse find	逆序查找
	}

	{
		//insert
		string s = "hello";

		string s1 = "world";
		//取值范围为 [0, s.size()]	闭区间
		s.insert(s.size(), s1);	//在原串下标为1的字符前插入 字符串s1
		//helloworld

		char c = 'w';
		s.insert(1, 2, c);	//在原串下标为1的字符前插入 2个 字符c
		//hwwelloworld

		s.insert(0, s1, 1, 3);	//在原串下标为1的字符前插入 从下标1 往后数 3个 字符的 字符串s1 
		//orlhwwelloworld
	}

	{
		//substr
		string s = "hello";
		string subs;
		subs = s.substr(1, 3);	//(pos, count)
		//ell

		/*
		* 经典用法: 字符串左移
		*/
		subs = (s + s).substr(1, s.size());	//左移 1 个字符
		//elloh
	}

	{
		//replace
		//用str替换指定字符串从起始位置pos开始长度为len的字符 
		//string& replace(size_t pos, size_t len, const string & str);
		string s = "12345";
		s = s.replace(2, 3, "aa");   //s="12aa";

		//用str替换 迭代器起始位置 和 结束位置 的字符 
		//string& replace(const_iterator i1, const_iterator i2, const string & str);

		string ss = "12345";
		ss = ss.replace(ss.begin(), ss.begin() + 3, "aaa");  //s="aaa45";

		string s1("Real Steel");
		s1.replace(1, 3, "123456", 2, 4);  //用 "123456" 的子串(2,4) 替换 s1 的子串(1,3)
		//R3456 Steel

		string s2("Harry Potter");
		s2.replace(2, 3, 5, '0');  //用 5 个 '0' 替换子串(2,3)
		//HaOOOOO Potter

		int n = s2.find("00000");  //查找子串 "00000" 的位置，n=2
		s2.replace(n, 5, "XXX");  //将子串(n,5)替换为"XXX"
		//HaXXX Potter
	}

	{
		//to_string
		int a = 4;
		double b = 3.14159265357989;
		string str1, str2;
		str1 = to_string(a);
		str2 = to_string(b);	//float浮点精度	%f
	}

	{
		//stoi的形参是const string*，而atoi的形参是const char*
		string s1("123456");
		const char* c1 = "123456";

		int a = stoi(s1);
		int b = atoi(c1);

		//double
		string s3("3.1415926535");
		double d = stod(s3);

		//进制转换(从其他进制转10进制)
		string s2("0x1234");
		int c = stoi(s1, 0, 16);	//c = 1193046
	}

	{
		//reverse	全局函数
		string s("123456");
		reverse(s.begin(), s.end());
	}
}